package com.zhentao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhentao.pojo.TcUser;
import com.zhentao.service.TcUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("tea/user")
public class UserController {

    @Autowired
    TcUserService userService;

    /**
     * 获取全部用户
     */
    @GetMapping("list")
    public List<TcUser> list(){
        return userService.list();
    }

    /**
     * 根据ID获取用户详情
     */
    @GetMapping("get/{id}")
    public TcUser getById(@PathVariable Integer id){
        return userService.getById(id);
    }

    /**
     * 添加用户
     */
    @PostMapping("add")
    public Map<String, Object> add(@RequestBody TcUser user){
        Map<String, Object> result = new HashMap<>();
        try {
            user.setCreateTime(new Date());
            user.setUpdateTime(new Date());
            boolean success = userService.save(user);
            result.put("success", success);
            result.put("message", success ? "添加成功" : "添加失败");
            if (success) {
                result.put("data", user);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "添加失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 更新用户
     */
    @PutMapping("update")
    public Map<String, Object> update(@RequestBody TcUser user){
        Map<String, Object> result = new HashMap<>();
        try {
            user.setUpdateTime(new Date());
            boolean success = userService.updateById(user);
            result.put("success", success);
            result.put("message", success ? "更新成功" : "更新失败");
            if (success) {
                result.put("data", user);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 删除用户
     */
    @DeleteMapping("delete/{id}")
    public Map<String, Object> delete(@PathVariable Integer id){
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = userService.removeById(id);
            result.put("success", success);
            result.put("message", success ? "删除成功" : "删除失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 根据身份类型获取用户
     */
    @GetMapping("byIdentity/{identity}")
    public List<TcUser> getByIdentity(@PathVariable String identity){
        QueryWrapper<TcUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("identity", identity);
        queryWrapper.orderByDesc("create_time");
        return userService.list(queryWrapper);
    }

    /**
     * 根据手机号获取用户
     */
    @GetMapping("byPhone/{phone}")
    public TcUser getByPhone(@PathVariable String phone){
        QueryWrapper<TcUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        return userService.getOne(queryWrapper);
    }

    /**
     * 搜索用户
     */
    @GetMapping("search")
    public List<TcUser> searchUsers(@RequestParam String keyword){
        QueryWrapper<TcUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", keyword).or().like("phone", keyword);
        queryWrapper.orderByDesc("create_time");
        return userService.list(queryWrapper);
    }

    /**
     * 获取老师列表
     */
    @GetMapping("teachers")
    public List<TcUser> getTeachers(){
        QueryWrapper<TcUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("identity", "老师");
        queryWrapper.orderByDesc("create_time");
        return userService.list(queryWrapper);
    }

    /**
     * 获取学生列表
     */
    @GetMapping("students")
    public List<TcUser> getStudents(){
        QueryWrapper<TcUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("identity", "学生");
        queryWrapper.orderByDesc("create_time");
        return userService.list(queryWrapper);
    }

    /**
     * 获取家长列表
     */
    @GetMapping("parents")
    public List<TcUser> getParents(){
        QueryWrapper<TcUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("identity", "家长");
        queryWrapper.orderByDesc("create_time");
        return userService.list(queryWrapper);
    }

    /**
     * 批量删除用户
     */
    @DeleteMapping("batchDelete")
    public Map<String, Object> batchDelete(@RequestBody List<Integer> ids){
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = userService.removeByIds(ids);
            result.put("success", success);
            result.put("message", success ? "批量删除成功" : "批量删除失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "批量删除失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 更新用户头像
     */
    @PutMapping("updateAvatar/{id}")
    public Map<String, Object> updateAvatar(@PathVariable Integer id, @RequestParam String avatar){
        Map<String, Object> result = new HashMap<>();
        try {
            TcUser user = userService.getById(id);
            if (user != null) {
                user.setAvatar(avatar);
                user.setUpdateTime(new Date());
                boolean success = userService.updateById(user);
                result.put("success", success);
                result.put("message", success ? "头像更新成功" : "头像更新失败");
            } else {
                result.put("success", false);
                result.put("message", "用户不存在");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "头像更新失败：" + e.getMessage());
        }
        return result;
    }
}
